<?php

namespace App\Http\Controllers;

use App\Post;
use Illuminate\Http\Request;
use App\Comment;
use App\Zan;

class PostController extends Controller
{

    //列表
    public function index(){
        $posts = Post::orderBy('created_at', 'DESC')->withCount(['comments', 'zans'])->paginate(6);
        $posts->load('user');
        return view('post.index', compact('posts'));
    }

    //详情页 -- 模型绑定
    public function show(Post $post)
    {
        //模型关联预加载
        $post->load('comments');
        return view('post.show', compact('post'));
    }

    //创建页
    public function create(){
        return view('post.create');
    }

    //创建逻辑
    public function store(Request $request)
    {
        //验证
        $this->validate($request, [
            'title' => 'required|min:5',
            'content' => 'required',
        ]);
        //逻辑
        $user_id = \Auth::id();
        $params = array_merge(request(['title', 'content']), compact('user_id'));
        Post::create($params);
        //渲染
        return redirect('/posts');
    }

    //编辑页 -- 模型绑定
    public function edit(Post $post)
    {
        return view('post.edit', compact('post'));
    }

    //编辑逻辑 -- 模型绑定
    public function update(Post $post)
    {
        //验证
        $this->validate(request(), [
            'title' => 'required|min:5',
            'content' => 'required',
        ]);

        //作者才有更新权限
        $this->authorize('update', $post);

        //逻辑
        $post->title = request('title');
        $post->content = request('content');
        $post->save();

        //渲染
        return redirect("/posts/{$post->id}");
    }

    //删除
    public function delete(Post $post)
    {
        //作者才有删除权限
        $this->authorize('delete', $post);

        $post->delete();
        return redirect('/posts');
    }

    //图片上传
    public function imageUpload(Request $request)
    {
        $path = $request->file('wangEditorH5File')->storePublicly(md5(time()));

        return asset('storage/' . $path);
    }

    //提交评论
    public function comment(Post $post)
    {
        //验证
        $this->validate(request(), [
            'content' => 'required|min:3',
        ]);

        //逻辑
        $comment = new Comment();
        $comment->user_id = \Auth::id();
        $comment->content = request('content');
        $post->comments()->save($comment);

        //第二种方法
        /*Comment::create([
            'user_id' => \Auth::user() -> id,
            'post_id' => $post->id,
            'content' => $request->input('content'),
        ]);*/

        //渲染
        return back();
    }

    //赞
    public function zan(Post $post)
    {
        $param = [
            'user_id' => \Auth::id(),
            'post_id' => $post->id,
        ];

        Zan::firstOrCreate($param);

        return back();
    }

    //取消赞
    public function unzan(Post $post)
    {
        $post->zan(\Auth::id())->delete();

        return back();
    }

    //搜索
    public function search(Request $request)
    {
        //验证
        $this->validate($request, [
            'query' => 'required',
        ]);

        //逻辑
        $query = $request->input('query');
        $posts = Post::where('title', 'like', "%$query%")->withCount(['comments', 'zans'])->paginate(2);

        //渲染
        return view('post.search', compact('query', 'posts'));
    }




}
